﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class RegisterBLL
    {
        private static int registerID = 0;
        GaraDataContext DB = new GaraDataContext();

        public IQueryable SelectALL()
        {
            return (from r in DB.Registers
                    join c in DB.Cards
                        on r.CardID equals c.CardID into r1
                    from c2 in r1.DefaultIfEmpty()
                    join t in DB.Types
                        on c2.TypeID equals t.TypeID into r2
                    from c3 in r2.DefaultIfEmpty()
                    join cu in DB.Customers
                    on r.CustomerID equals cu.CustomerID into r3
                    from result in r3.DefaultIfEmpty()
                    select new
                    {
                        r.RegisterID,
                        r.CustomerID,
                        r.CardID,
                        Name = result.LastName + " " + result.FirstName,
                        c3.TypeName,
                        r.Fee,
                        CardStatus = c2.Status,
                        r.DateRegister,
                        r.DateExpiration,
                        r.Status
                    }).Distinct().OrderByDescending(s => s.RegisterID).AsQueryable();
        }

        public RegisterDTO LoadInfoRegister(string cardID)
        {
            return (from r in DB.Registers
                    join c in DB.Cards
                        on r.CardID equals c.CardID into r1
                    from c2 in r1.DefaultIfEmpty()
                    where c2.CardID == cardID
                    join t in DB.Types
                        on c2.TypeID equals t.TypeID into r2
                    from c3 in r2.DefaultIfEmpty()
                    join cu in DB.Customers
                    on r.CustomerID equals cu.CustomerID into r3
                    from result in r3.DefaultIfEmpty()
                    select new RegisterDTO
                    {
                        RegisterID = r.RegisterID,
                        CustomerID = r.CustomerID,
                        CardID = r.CardID,
                        Name = result.LastName + " " + result.FirstName,
                        TypeName = c3.TypeName,
                        Fee = r.Fee,
                        CardStatus = c2.Status,
                        DateRegister = Convert.ToDateTime(r.DateRegister),
                        DateExiration = Convert.ToDateTime(r.DateExpiration),
                        Status = r.Status
                    }).Distinct().OrderByDescending(s => s.RegisterID).FirstOrDefault<RegisterDTO>();
        }
        public string Insert(RegisterDTO registerDTO)
        {
            try
            {
                Register register = new Register();
                register.RegisterID = registerID++;
                register.CustomerID = registerDTO.CustomerID;
                register.CardID = registerDTO.CardID;
                register.DateRegister = registerDTO.DateRegister;
                register.DateExpiration = registerDTO.DateExiration;
                register.StaffID = registerDTO.StaffID;
                register.Fee = registerDTO.Fee;
                register.Status = registerDTO.Status;

                DB.Registers.InsertOnSubmit(register);
                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Update(RegisterDTO registerDTO)
        {
            try
            {
                Register register = DB.Registers.Where(s => s.RegisterID == registerDTO.RegisterID).FirstOrDefault();
                register.CustomerID = registerDTO.CustomerID;
                register.CardID = registerDTO.CardID;
                register.DateRegister = registerDTO.DateRegister;
                register.DateExpiration = registerDTO.DateExiration;
                register.StaffID = registerDTO.StaffID;

                DB.SubmitChanges();
                return MessageAction.Success;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public void UpdateStatusAllCard()
        {

        }

        public Boolean CheckCardIdRegister(string cardID)
        {
            int count = (from p in DB.Registers
                         where p.CardID == cardID
                         join c in DB.Cards
                         on p.CardID equals c.CardID into r
                         from result in r.DefaultIfEmpty()
                         where result.Status == "Chưa đăng ký"
                         select new
                         {
                             p.CustomerID
                         }).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public Boolean CheckCustomerRegister(string customerID)
        {
            int count = (from re in DB.Registers
                         join c in DB.Cards
                             on re.CardID equals c.CardID into r
                         from result in r.DefaultIfEmpty()
                         where result.Status != "Chưa đăng ký" && re.CustomerID == customerID
                         select result).Distinct().Count();
            if (count > 0) return true;
            else return false;
        }

        public RegisterDTO LoadInfoCardOfCustomer(string customerID)
        {
            return (from r in DB.Registers
                    join c in DB.Cards
                        on r.CardID equals c.CardID into r1
                    from c2 in r1.DefaultIfEmpty()
                    join t in DB.Types
                        on c2.TypeID equals t.TypeID into r2
                    from c3 in r2.DefaultIfEmpty()
                    join cu in DB.Customers
                    on r.CustomerID equals cu.CustomerID into r3
                    from result in r3.DefaultIfEmpty()
                    where result.CustomerID == customerID
                    select new RegisterDTO
                    {
                        RegisterID = r.RegisterID,
                        CustomerID = r.CustomerID,
                        CardID = r.CardID,
                        Name = result.LastName + " " + result.FirstName,
                        TypeName = c3.TypeName,
                        Fee = r.Fee,
                        CardStatus = c2.Status,
                        DateRegister = Convert.ToDateTime(r.DateRegister),
                        DateExiration = Convert.ToDateTime(r.DateExpiration),
                        Status = r.Status
                    }).Distinct().OrderByDescending(s => s.RegisterID).FirstOrDefault<RegisterDTO>();
        }
    }
}
